<template>
  <div class="user-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      left-arrow
      title="用户名"
      @click-left="$router.back()"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <div class="user-info">
      <div class="base-info">
        <van-image
          class="avatar"
          round
          fit="cover"
          :src="user.photo"
        />
        <div class="right-area">
          <div class="stats-wrap">
            <div class="stats-item">
              <span class="count">{{user.art_count}}</span>
              <span class="text">发布</span>
            </div>
            <div class="stats-item">
              <span class="count">{{user.follow_count}}</span>
              <span class="text">关注</span>
            </div>
            <div class="stats-item">
              <span class="count">{{user.fans_count}}</span>
              <span class="text">粉丝</span>
            </div>
            <div class="stats-item">
              <span class="count">{{user.like_count}}</span>
              <span class="text">获赞</span>
            </div>
          </div>
          <van-button
          class="follow-btn"
          @click="handleFollowUser"
          >{{user.is_following === true ? '已关注' : '关注'}}</van-button>
        </div>
      </div>
    </div>
    <div class="intro-wrap">
      <div class="identi">
        <span class="title">认证：</span>
        <span class="title-info">{{user.certi === '' ? '该用户还未认证' : user.certi}}</span>
      </div>
      <div>
        <span class="title">简介：</span>
        <span class="title-info">{{user.intro}}</span>
      </div>
    </div>
    <UserContent
    @userInfo="user=$event"
    :userId="userId"
    />
  </div>
</template>

<script>
import UserContent from './components/user-content'
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'UserIndex',
  components: {
    UserContent
  },
  data () {
    return {
      userId: this.$route.params.userId,
      user: {}
    }
  },
  methods: {
    async handleFollowUser () {
      try {
        if (this.user.is_following === false) {
          await addFollow(this.userId)
          this.user.is_following = true
          this.$toast.success('关注成功')
        } else {
          await deleteFollow(this.userId)
          this.user.is_following = false
          this.$toast.success('取关成功')
        }
      } catch (err) {
        this.$toast('操作失败，稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.user-container {
  .user-info {
    background-color: #fff;
    padding: 25px 32px;
    .base-info {
      display: flex;
      margin-bottom: 25px;
      .avatar {
        width: 155px;
        height: 155px;
        margin-right: 62px;
      }
    }
  }
  .intro-wrap {
    margin-top: 10px;
    padding: 20px;
    font-size: 25px;
    background-color: #fff;
    .identi {
      margin-bottom: 20px;
    }
    .title {
      color: #646263;
    }
    .title-info {
      color: #212121;
    }
  }
  .right-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    .stats-wrap {
      display: flex;
      justify-content: space-between;
      .stats-item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .count {
          font-size: 40px;
          color: #0d0a10;
        }
        .text {
          font-size: 21px;
          color: #9c9b9d;
        }
      }
    }
    .follow-btn {
      width: 289px;
      height: 55px;
      line-height: 55px;
      background-color: #6bb5ff;
      color: #fff;
      border: none;
      border-radius: 20px;
    }
  }
}
</style>
